package me.zhengjie.pm.issuerelated.service.impl;

import me.zhengjie.pm.issuerelated.domain.IssueRelated;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.issuerelated.repository.IssueRelatedRepository;
import me.zhengjie.pm.issuerelated.service.IssueRelatedService;
import me.zhengjie.pm.issuerelated.service.dto.IssueRelatedDto;
import me.zhengjie.pm.issuerelated.service.dto.IssueRelatedQueryCriteria;
import me.zhengjie.pm.issuerelated.service.mapper.IssueRelatedMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yuxj
* @date 2020-05-02
*/
@Service
//@CacheConfig(cacheNames = "issueRelated")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IssueRelatedServiceImpl implements IssueRelatedService {

    private final IssueRelatedRepository issueRelatedRepository;

    private final IssueRelatedMapper issueRelatedMapper;

    public IssueRelatedServiceImpl(IssueRelatedRepository issueRelatedRepository, IssueRelatedMapper issueRelatedMapper) {
        this.issueRelatedRepository = issueRelatedRepository;
        this.issueRelatedMapper = issueRelatedMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(IssueRelatedQueryCriteria criteria, Pageable pageable){
        Page<IssueRelated> page = issueRelatedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(issueRelatedMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<IssueRelatedDto> queryAll(IssueRelatedQueryCriteria criteria){
        return issueRelatedMapper.toDto(issueRelatedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public IssueRelatedDto findById(Integer id) {
        IssueRelated issueRelated = issueRelatedRepository.findById(id).orElseGet(IssueRelated::new);
        ValidationUtil.isNull(issueRelated.getId(),"IssueRelated","id",id);
        return issueRelatedMapper.toDto(issueRelated);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public IssueRelatedDto create(IssueRelated resources) {
        return issueRelatedMapper.toDto(issueRelatedRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(IssueRelated resources) {
        IssueRelated issueRelated = issueRelatedRepository.findById(resources.getId()).orElseGet(IssueRelated::new);
        ValidationUtil.isNull( issueRelated.getId(),"IssueRelated","id",resources.getId());
        issueRelated.copy(resources);
        issueRelatedRepository.save(issueRelated);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            issueRelatedRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<IssueRelatedDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (IssueRelatedDto issueRelated : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("原问题ID", issueRelated.getIssueId());
            map.put("关联型号", issueRelated.getRelatedType());
            map.put("关联项目ID", issueRelated.getRelatedProject());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}